sort map keys dart

90

import 'dart:collection';

void main() {
  final sortedMap = SplayTreeMap<DateTime, double>((a, b) => b.compareTo(a));

  sortedMap[DateTime(2021, 1, 1)] = 1.0;
  sortedMap[DateTime(2021, 1, 3)] = 3.0;
  sortedMap[DateTime(2021, 1, 4)] = 4.0;
  sortedMap[DateTime(2021, 1, 2)] = 2.0;
  sortedMap[DateTime(2021, 1, 5)] = 5.0;

  sortedMap.entries.forEach(print);
  // MapEntry(2021-01-01 00:00:00.000: 1.0)
  // MapEntry(2021-01-02 00:00:00.000: 2.0)
  // MapEntry(2021-01-03 00:00:00.000: 3.0)
  // MapEntry(2021-01-04 00:00:00.000: 4.0)
  // MapEntry(2021-01-05 00:00:00.000: 5.0)
}

Comments

Submit
0 Comments